<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item>
        <ul class="chapterList">
          <li v-for="chapter in chapterNestedList" :key="chapter.id">
            <p>
              {{ chapter.title }}
            </p>

            <!-- 视频 -->
            <ul class="chapterList videoList">
              <li v-for="video in chapter.children" :key="video.id">
                <p>
                  {{ video.title }}
                  <span class="acts">
                    <el-button type="text" @click="showVideo(video.videoSourceId)">
                      查看
                    </el-button>
                  </span>
                </p>
              </li>
            </ul>
          </li>
        </ul>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import chapterApi from '@/api/edu/chapter'
import videoApi from '@/api/edu/video'
import vodApi from "@/api/edu/vod"
export default {
  data() {
    return {
      BASE_API: process.env.VUE_APP_BASE_API,
      chapter: {
        // 章节对象
        title: '',
        sort: 0
      },
      courseId: '', // 所属课程
      chapterNestedList: [], // 章节嵌套课时列表
      chapterId: '', // 课时所在的章节id
      video: {
        id: '',
        // 课时对象
        title: '',
        sort: 0,
        free: 0,
        videoSourceId: '',
        videoOriginalName: ''
      }
    }
  },

  created() {
    console.log('chapter created')
    this.init()
  },

  methods: {
    //初始化页面
    init() {
      if (this.$route.params && this.$route.params.id) {
        this.courseId = this.$route.params.id
        // 根据id获取课程基本信息
        this.fetchChapterNestedListByCourseId()
      }
    },

    //获取章节列表
    fetchChapterNestedListByCourseId() {
      chapterApi.getNestedTreeList(this.courseId).then(response => {
        this.chapterNestedList = response.data.items
      })
    },

    //播放视频
    async showVideo(vid) {
      var playAuth = ""
      await vodApi.getPlayAuth(vid).then(response => {
        playAuth = response.data.playAuth
      })
      localStorage.setItem("playAuth",playAuth)
      this.$router.push({ path: '/edu/course/player/' + vid })
    }
  }
}
</script>

<style scoped>
.chapterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chapterList li {
  position: relative;
}
.chapterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}
.chapterList .acts {
  float: right;
  font-size: 14px;
}

.videoList {
  padding-left: 50px;
}
.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #ddd;
}
</style>
